package indi.gao.localtime;

import org.junit.Test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * @author :gaolei
 * @version :v1.0
 * @projectName :GLUtil
 * @description :
 * @createDate :2021/5/22 10:17 上午
 */
public class LocalTimeTest {

    @Test
    public void testLocal() throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = simpleDateFormat.parse("2021-05-21 10:50:11");
        LocalTime localTime = timeLocalTime(parse);
        System.out.println(localTime.isAfter(timeLocalTime(null)));
    }

    public static LocalTime timeLocalTime(Date time) {
        if (time == null) {
            return LocalTime.now();
        }
        Instant instant = time.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。)
        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
        LocalTime localTime = instant.atZone(zoneId).toLocalTime();
        return localTime;
    }

    @Test
    public void getSecondUntilTom() {
        //从一个 Instant和区域ID获得 LocalDateTime实例
        LocalDateTime localDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
        //获取第第二天零点时刻的实例
        LocalDateTime toromorrowTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        //ChronoUnit日期枚举类,between方法计算两个时间对象之间的时间量
        long seconds = ChronoUnit.SECONDS.between(localDateTime, toromorrowTime);
        System.out.println(seconds);
    }
}
